package com.java.service;

import com.java.dao.*;
import com.java.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service("makerService")
public class Maker1ServiceImpl implements Maker1Service {
    @Autowired
    private Maker1Mapper maker1Mapper;

    @Autowired
    private Maker2Mapper maker2Mapper;

    @Autowired
    private Maker3Mapper maker3Mapper;

    @Autowired
    private EnterGoodsMapper enterGoodsMapper;

    @Autowired
    private Bank2Mapper bank2Mapper;

    @Autowired
    private IntoAndOutMapper intoAndOutMapper;

    @Autowired
    private TrackingMapper trackingMapper;


    @Override
    public Page<Maker1> getPageByPage(Integer a, String forname) {
        int b = 4;//显示条数
        Page<Maker1> page = new Page<>();
        page.setCurryPage(a);//设置当前页码
        page.setRows(b);//设置每页显示的条数
        Integer count = maker1Mapper.getCount(forname);//总条数
        page.setTotalCount(count);//总条数
        int start = (a - 1) * b;/*开始的索引*/
        List<Maker1> list = maker1Mapper.getListEnterGood(start, b, forname);
        page.setList(list);/*赋值list*/
        int totalPae = count % b == 0 ? count / b : (count / b) + 1;
        page.setTotalPae(totalPae);/*设置总页数*/
        if (page == null) {
            page = null;
        }
        return page;
    }

    @Override
    public Integer getCount(String forname) {
        return maker1Mapper.getCount(forname);
    }

    @Override
    public int addMaker1(Maker1 maker1) {
        return maker1Mapper.addMaker1(maker1);
    }

    @Override
    public int updateMaker(Integer mid, Integer num, Double money, String goodname) {
        return maker1Mapper.updateMaker(mid, num, money, goodname);
    }

    @Override
    public Maker1 getMaker1ByMid(Integer mid) {
        return maker1Mapper.getMaker1ByMid(mid);
    }


    @Override
    public int deleteMaker1(Integer mid) {
        Maker1 maker1ByMid = maker1Mapper.getMaker1ByMid(mid);
        //外键获取list maker2
        List<Maker2> m2ByM1id = maker2Mapper.getM2ByM1id(mid);
        for (int j=0;j<m2ByM1id.size();j++){
            int i = enterGoodsMapper.updOknandNo3_2(m2ByM1id.get(j).getGoods2());
        }
        int i = maker2Mapper.deleteMaker2(mid);
        int i2 = maker1Mapper.deleteMaker1(mid);
        return i2;
    }

    @Override
    public Maker1 getMaker1AndMaker2List(Integer id) {
        return maker1Mapper.getMaker1AndMaker2List(id);
    }


    //销售
    @Override
    public boolean MAK(Integer mid) {
        //根据maker1主键,一多多联查
        Maker1 maker1AndMaker2List = maker1Mapper.getMaker1AndMaker2List(mid);
        List<Maker2> list = maker1AndMaker2List.getMaker2List();
        System.out.println("maker1AndMaker2List:" + maker1AndMaker2List);
        System.out.println("list:"+list);

        //根据主键查询 有则直接修改处理状态 没有则重新添加
        int i1=0;
        Tracking trByName = trackingMapper.getTRByID(maker1AndMaker2List.getMid());
        System.out.println("====="+trByName);
        if(trByName==null){
        //创建订单的追踪的对象
        Tracking tracking=new Tracking();
        tracking.setDocument1(maker1AndMaker2List.getDocument());
        tracking.setGoodstyle(maker1AndMaker2List.getGoodstyle());
        tracking.setGoods(maker1AndMaker2List.getGoodname());
        tracking.setNum(maker1AndMaker2List.getNum());
        tracking.setDatat(maker1AndMaker2List.getData1());
        tracking.setForname2(maker1AndMaker2List.getForname());
        tracking.setM1id(maker1AndMaker2List.getMid());
        tracking.setWhe2(maker1AndMaker2List.getWhe());
        //生成随机数
        int min = 1;
        int max = 6;

        Random random = new Random();

        int value = random.nextInt(max + min) + min;
        tracking.setNumdata(value);
        System.out.println("000:"+tracking);
        //操作,创建Tracking对象i1 = trackingMapper.addTR(tracking);
            trackingMapper.addTR(tracking);
        }else {
            trackingMapper.updOKN2(maker1AndMaker2List.getMid());
        }


        //变量
        int m = 0;//创建maker3  ||  修改maker3的okandno:是否退货
        int e = 0;//修改entergoods的okandno:是否销售出去
        int b = 0;//添加bank2


        //判断maker3是否存在,根据外键查询
        List<Maker3> list2 = maker3Mapper.getMaker3ByM1Id(maker1AndMaker2List.getMid());
        if (list2.size() == 0) {
            //创建maker3
            for (int i = 0; i < list.size(); i++) {
                Maker3 maker3 = new Maker3();
                maker3.setM1id(maker1AndMaker2List.getMid());//外键
                maker3.setDocument1(maker1AndMaker2List.getDocument());
                maker3.setDocument2(list.get(i).getDocuemnt2());
                maker3.setData3(maker1AndMaker2List.getData1());
                maker3.setForname(maker1AndMaker2List.getForname());
                maker3.setGoods(list.get(i).getGoods2());
                maker3.setNum(list.get(i).getNum());
                maker3.setDiscountmoney(list.get(i).getDiscountmoney());
                maker3.setCountmoney(list.get(i).getCountmoney());
                m = maker3Mapper.addMaker3(maker3);
            }
        } else if (list2.size() > 0) {
            m = maker3Mapper.updOkandNo(maker1AndMaker2List.getMid());
        }


        //修改entergoods的okandno:是否销售出去
        for (int j = 0; j < list.size(); j++) {
            e = enterGoodsMapper.updOkAndNo(list.get(j).getGoods2());
        }


        Bank2 bank21 = bank2Mapper.getBank2ByGood(maker1AndMaker2List.getGoodname());
        if(bank21==null) {
            //创建bank2,账单
            for (int k = 0; k < list.size(); k++) {
                Bank2 bank2 = new Bank2();
                Double dis = list.get(k).getDiscountmoney();//折扣后
                Double con = list.get(k).getCountmoney();//折扣前
                Double jian = con - dis;
                bank2.setDiscountmoney(dis);
                bank2.setCountmoney(con);
                bank2.setProfit(jian);
                bank2.setGoods(list.get(k).getGoods2());
                bank2.setForname(maker1AndMaker2List.getForname());
                b = bank2Mapper.addBank2(bank2);
            }
        }else {
            b=2;
        }

        //修改maker1的状态
        int k2 = maker1Mapper.updMK(mid);


        //修改仓库的okandno==1
        int i = intoAndOutMapper.upd2RK(list.get(0).getGoods2());
        System.out.println("i:"+i+"m:"+m+"e:"+e+"b:"+b+"k2:"+k2+"i1:"+i1);


        if (m > 0 && e > 0 && b>0&&k2>0 || m > 0 && e > 0 && b>2&&k2>0 ) {
            return true;
        } else {
            return false;
        }
    }

    //退货
    @Override
    public boolean THM(Integer mid) {
        //根据maker1主键,一多多联查
        Maker1 maker1AndMaker2List = maker1Mapper.getMaker1AndMaker2List(mid);
        List<Maker2> list = maker1AndMaker2List.getMaker2List();
        System.out.println("maker1AndMaker2List:" + maker1AndMaker2List);


        //修改maker1的okandno=0,退货
        int i3 = maker1Mapper.updTH(mid);

        //修改tracking的处理状态
        int i4 = trackingMapper.updOKN(mid);

        //修改maker3的okandno:退货  ==0
        int i = maker3Mapper.updOKandNoMaker3(mid);

        //创建一个变量
        int i1 = 0;//修改entergoods的okandno(是否销售出去)==0

        //修改entergoods的okandno(是否销售出去)==0
        for (int j = 0; j < list.size(); j++) {
            i1 = enterGoodsMapper.updOkandNoGoods(list.get(j).getGoods2());
        }

        //删除该账单
        int i2 = 0;
        for (int o=0;o<list.size();o++){
           i2= bank2Mapper.delBank2(list.get(o).getGoods2(),maker1AndMaker2List.getForname());
        }

        //仓库出库改为入库
        int i5 = intoAndOutMapper.updOutUPInt(list.get(0).getGoods2());
        System.out.println("------------------------"+i5+list.get(0).getGoods2());

        System.out.println("maker1:"+maker1AndMaker2List);
        System.out.println("list:"+list);
        System.out.println("i:"+i);
        System.out.println("i1:"+i1);
        System.out.println("i2:"+i2);
        System.out.println("i3:"+i3);

        if (i > 0 && i1 > 0&&i2>0&&i3>0 &&i4>0) {
            return true;
        } else{
            return false;
    }
}
}
